package b2.widget.task
{
	import b2.config.*;
	import b2.net.*;
	import b2.player.*;
	import b2.scene.*;
	import b2.scene.camp.*;
	import b2.util.*;
	import b2.widget.*;
	import b2.widget.fort.*;
	import b2.widget.login.*;
	
	import flash.display.Sprite;
	import flash.events.*;
	import flash.filters.*;
	import flash.text.*;

	public class TaskDetailPane extends Sprite
	{
		private var titleText:TextField;
		private var descripText:TextField;
		private var requireDetailText:TextField;
		private var processText:TextField;
		private var awardItems:Array;
		private var taskDialog:TaskDialog;
		private var contentContainer:Sprite;
		
		public function TaskDetailPane(taskDialog:TaskDialog)
		{
			super();
			this.taskDialog = taskDialog;
			//线框
			var lineRec:Sprite = new Sprite();
			lineRec.graphics.lineStyle(2, 0x27AAE1);
			lineRec.graphics.drawRoundRect(4, 20, 300, 138, 20, 20);
			lineRec.graphics.drawRoundRect(4, 158, 300, 60, 20, 20);
			lineRec.graphics.drawRoundRect(4, 218, 300, 175, 20, 20);
			lineRec.graphics.drawRoundRect(4, 393, 300, 50, 20, 20);
			lineRec.filters = [new DropShadowFilter(2, 44, 0x000000, 1, 2, 2, 1, 1, false)];
			addChild(lineRec);
			//线框色块
			var rrect:Sprite = new Sprite();
			rrect.graphics.beginFill(0x27AAE1);
			rrect.graphics.drawRoundRect(6, 22, 296, 25, 15, 15);
			rrect.graphics.drawRoundRect(6, 160, 296, 25, 15, 15);
			rrect.graphics.drawRoundRect(6, 220, 296, 25, 15, 15);
			rrect.graphics.endFill();
			addChild(rrect);
			//标题
			titleText = new TextField();
			titleText.selectable = false;
        	titleText.autoSize = TextFieldAutoSize.CENTER;
			titleText.defaultTextFormat = new TextFormat("微软雅黑", 15, 0xFFFF00);
			titleText.filters = [new GlowFilter(0x000000, 1, 1.3, 1.3, 100, BitmapFilterQuality.HIGH)];
			titleText.x = 140;
			titleText.y = 20;
			addChild(titleText);
			//任务详情
			descripText = new TextField();
			descripText.selectable = false;
        	descripText.autoSize = TextFieldAutoSize.LEFT;
        	descripText.wordWrap = true;
        	descripText.width = 280;
			descripText.defaultTextFormat = new TextFormat("微软雅黑", 13, 0xFFFFFF);
			descripText.filters = [new GlowFilter(0x000000, 1, 1.3, 1.3, 100, BitmapFilterQuality.HIGH)];
			descripText.x = 14;
			descripText.y = 50;
			addChild(descripText);
			//任务要求
			var requirementText:TextField = new TextField();
			requirementText.selectable = false;
        	requirementText.autoSize = TextFieldAutoSize.LEFT;
			requirementText.defaultTextFormat = new TextFormat("微软雅黑", 15, 0xFFFF00);
			requirementText.filters = [new GlowFilter(0x000000, 1, 1.3, 1.3, 100, BitmapFilterQuality.HIGH)];
			requirementText.text = "任务要求";
			requirementText.x = 110;
			requirementText.y = 160;
			addChild(requirementText);
			//任务具体要求
			requireDetailText = new TextField();
			requireDetailText.selectable = false;
        	requireDetailText.autoSize = TextFieldAutoSize.LEFT;
			requireDetailText.defaultTextFormat = new TextFormat("微软雅黑", 14, 0xFF0000);
			requireDetailText.filters = [new GlowFilter(0xFFFFFF, 1, 1.3, 1.3, 100, BitmapFilterQuality.HIGH)];
			requireDetailText.x = 14;
			requireDetailText.y = 190;
			addChild(requireDetailText);
			//进度
			processText = new TextField();
			processText.selectable = false;
        	processText.autoSize = TextFieldAutoSize.LEFT;
			processText.defaultTextFormat = new TextFormat("微软雅黑", 14, 0x000000);
			processText.filters = [new GlowFilter(0xFFFFFF, 1, 1.3, 1.3, 100, BitmapFilterQuality.HIGH)];
			processText.y = 190;
			addChild(processText);
			//任务奖励
			var awardText:TextField = new TextField();
			awardText.selectable = false;
        	awardText.autoSize = TextFieldAutoSize.LEFT;
			awardText.defaultTextFormat = new TextFormat("微软雅黑", 15, 0xFFFF00);
			awardText.filters = [new GlowFilter(0x000000, 1, 1.3, 1.3, 100, BitmapFilterQuality.HIGH)];
			awardText.text = "任务奖励";
			awardText.x = 110;
			awardText.y = 220;
			addChild(awardText);
		}
		
		public function refresh():void
		{
			if (contentContainer != null && contentContainer.parent != null)
				contentContainer.parent.removeChild(contentContainer);
			titleText.text = "没有任务";				
			descripText.text = "";
			requireDetailText.text = "";
			processText.text = ""
			if (taskDialog.currentSelectedTaskItem != null)
			{
				contentContainer = new Sprite();
				addChild(contentContainer);
				var taskIndex:int = taskDialog.currentSelectedTaskItem.taskIndex;
				var taskConfig:TaskConfig = Config.taskConfigs[taskIndex];
				var me:PlayerInfo = PlayerInfoManager.getInstance().players[PlayerInfoManager.getInstance().myUserId];
				var taskStatus:int = me.getTaskStatus(taskIndex);
				titleText.htmlText = taskConfig.name;
				//设置任务详情
				descripText.multiline = true;
				descripText.htmlText = taskConfig.description;
				if (taskConfig.objectIndex >= 0)
				{
					var objectConfig:CommonObjConfig = Config.objectConfigs[taskConfig.objectIndex];
					var objBasicLevel:int = taskConfig.objectQuality / 5;
					var objStrengthLevel:int = taskConfig.objectQuality % 5;
					var basicLevelName:String = Config.basicConfig.objectBasicLevel[objBasicLevel];
					var strengthenLevelName:String = Config.basicConfig.objectStrengthenLevel[objStrengthLevel];
					var objectName:String;
					if (objectConfig.noBasicLevel)
						objectName = objectConfig.name;
					else if (objectConfig.noStrengthenLevel)
						objectName = basicLevelName + " 的 " + objectConfig.name;
					else
						objectName = basicLevelName + strengthenLevelName + " 的 " + objectConfig.name;
				}
				//动作按钮
				if (taskStatus != -2)
				{ 
					//战役类任务
					if (taskConfig.goal == 0)
					{
						var campConfig:CampConfig = Config.campConfigs[taskConfig.campIndex];
						requireDetailText.text = "完成战役 “" +  campConfig.name + "” " + taskConfig.campNum + " 场";
						if (taskStatus >= 0)
							processText.text = me.getCampNum(taskConfig.campIndex) + "/" + taskConfig.campNum;
					}
					//收集类任务
					else if (taskConfig.goal == 1)
					{ 	
						requireDetailText.text = "收集 “" + objectName + "” " + taskConfig.objectNum + " 个";
						if (taskStatus >= 0)
							processText.text = me.getFinishTaskObjNum(taskConfig.objectIndex, objBasicLevel, objStrengthLevel) + "/" + 
								taskConfig.objectNum;
					}
					//装备
					else if (taskConfig.goal == 2)
					{
						requireDetailText.text = "将 “" + objectName + "” 装备到坦克上";
					}
					//强化装备
					else if (taskConfig.goal == 3)
					{
						requireDetailText.text = "强化 “" + objectName + "” " + taskConfig.objectNum + " 个";
						if (taskStatus >= 0)
							processText.text = me.getFinishTaskObjNum(taskConfig.objectIndex, objBasicLevel, objStrengthLevel) + "/" + 
								taskConfig.objectNum;
					}
					//合成装备（勋章）
					else if (taskConfig.goal == 4 || taskConfig.goal == 7)
					{
						requireDetailText.text = "合成 “" + objectName + "” " + taskConfig.objectNum + " 个";
						if (taskStatus >= 0)
							processText.text = me.getFinishTaskObjNum(taskConfig.objectIndex, objBasicLevel, objStrengthLevel) + "/" + 
								taskConfig.objectNum;
					}
					//强化坦克
					else if (taskConfig.goal == 5)
					{
						requireDetailText.text = "强化 “" + basicLevelName + strengthenLevelName + " 的 坦克” " + taskConfig.objectNum + " 辆";
						if (taskStatus >= 0)
							processText.text = me.getFinishTaskTankNum(objBasicLevel, objStrengthLevel) + "/" + taskConfig.objectNum;
					}
					//合成坦克
					else if (taskConfig.goal == 6)
					{
						requireDetailText.text = "合成 “" + basicLevelName + strengthenLevelName + " 的 坦克” " + taskConfig.objectNum + " 辆";
						if (taskStatus >= 0)
							processText.text = me.getFinishTaskTankNum(objBasicLevel, objStrengthLevel) + "/" + taskConfig.objectNum;
					}
					if (taskConfig.requirement != "")
						requireDetailText.text = taskConfig.requirement;
					processText.x = 290 - processText.width;
				}
				else
				{
					requireDetailText.text = "需要等级 " + taskConfig.requireLevel;
				}
				//任务奖励
				awardItems = new Array();
				for (var i:int = 0; i < taskConfig.awardObj.length; i++)
				{
					var awardItem:GiftItem = new GiftItem([taskConfig.awardObj[i]]);
					awardItem.x = 15 + (i % 2) * 140;
					awardItem.y = 250 + int(i / 2) * 70;
					contentContainer.addChild(awardItem);
					awardItems.push(awardItem);
				}
				var actionButton:BasicButton;
				//动作按钮
				if (taskStatus != -2)
				{ 
					var ButtonBgr:Class = ResourceHelper.getInstance().getClassByName("ButtonBgr1");
					//未接任务
					if (taskStatus == -1)
					{
						actionButton = new BasicButton(ButtonBgr, "接受任务");
						actionButton.addEventListener(MouseEvent.CLICK, onAcceptTaskClick);
					}
					//进行中
					else if (taskStatus == 0)
					{
						actionButton = new BasicButton(ButtonBgr, "立即开始");
						actionButton.addEventListener(MouseEvent.CLICK, onStartTaskClick);
					}
					//完成任务
					else
					{
						actionButton = new BasicButton(ButtonBgr, "完成任务");
						actionButton.addEventListener(MouseEvent.CLICK, onAccomplishTaskClick);
					}
					actionButton.x = 110;
					actionButton.y = 398;
					contentContainer.addChild(actionButton);
				}
			}
		}
		
		//接受任务
		private function onAcceptTaskClick(event:MouseEvent):void
		{
			var actionButton:BasicButton = event.currentTarget as BasicButton;
			if (actionButton.disable)
				return;
			actionButton.disable = true;
			var taskIndex:int = taskDialog.currentSelectedTaskItem.taskIndex;
			var taskConfig:TaskConfig = Config.taskConfigs[taskIndex];
			ServerHelper.getInstance().acceptTask(taskIndex, taskConfig.campIndex);
			ServerHelper.getInstance().addEventListener(ServerEvent.ACCEPT_TASK, onAcceptTaskComplete);
		}
		
		private function onAcceptTaskComplete(event:ServerEvent):void
		{
			ServerHelper.getInstance().removeEventListener(ServerEvent.ACCEPT_TASK, onAcceptTaskComplete);
        	var msgBox:PopMessage = new PopMessage("成功接受任务 \“" + titleText.text +"\”", 0x00ff00, 20);
        	WidgetManager.getInstance().popOut([msgBox], false, true);
			refresh();
			taskDialog.currentSelectedTaskItem.refresh();
		}
		
		//立即开始
		private function onStartTaskClick(event:MouseEvent):void
		{
			var actionButton:BasicButton = event.currentTarget as BasicButton;
			if (actionButton.disable)
				return;
			actionButton.disable = true;
			var taskIndex:int = taskDialog.currentSelectedTaskItem.taskIndex;
			var taskConfig:TaskConfig = Config.taskConfigs[taskIndex];
			//装备
			if (taskConfig.goal == 2)
			{
				BottomBar.showWarehouse(true);
			}
			//强化装备
			else if (taskConfig.goal == 3)
			{
				BottomBar.showTechTower(0, 1, true);
			}
			//合成装备
			else if (taskConfig.goal == 4)
			{
				BottomBar.showTechTower(1, 1, true);
			}
			//强化坦克
			else if (taskConfig.goal == 5)
			{
				BottomBar.showTechTower(0, 0, true);
			}
			//合成坦克
			else if (taskConfig.goal == 6)
			{
				BottomBar.showTechTower(1, 0, true);
			}
			//合成勋章
			else if (taskConfig.goal == 7)
			{
				BottomBar.showTechTower(1, 2, true);
			}
			//战役或者收集
			else
			{
				//收集荣誉
				if (taskConfig.objectIndex == CommonObjConfig.HONOUR)
				{
					BottomBar.onFriendIconClick(null);
				}
				else
				{
					if (!(SceneManager.getInstance().currentScene is CampScene))
						SceneManager.getInstance().loadScene(CampScene.getInstance());
					var fortIndex:int = Config.campFortLink[taskConfig.gotoCampIndex];
					var fortressDialog:FortressDialog = new FortressDialog(fortIndex, taskConfig.gotoCampIndex);
					fortressDialog.x = 70;
					fortressDialog.y = (BFont.stageHeight - fortressDialog.height) / 2;
					WidgetManager.getInstance().popOut([fortressDialog], true);
				}
			}
			TaskDialog.getInstance().close();
		}
		
		//完成任务
		private function onAccomplishTaskClick(event:MouseEvent):void
		{
			var actionButton:BasicButton = event.currentTarget as BasicButton;
			if (actionButton.disable)
				return;
			actionButton.disable = true;
			var taskIndex:int = taskDialog.currentSelectedTaskItem.taskIndex;
			var objectBalance:String = "";
			for (var i:int = 0; i < awardItems.length; i++)
			{
				var awardItem:GiftItem = awardItems[i];
				if (i > 0)
					objectBalance += ",";
				objectBalance += PlayerInfoManager.getInstance().myUserId + "," + awardItem.objectIndex + "," + awardItem.objBasicLevel + "," + 
					awardItem.objStrengthenLevel + ",0," + awardItem.objNum;
			}
			ServerHelper.getInstance().accomplishTask(taskIndex, objectBalance);
			ServerHelper.getInstance().addEventListener(ServerEvent.ACCOMP_TASK, onAccomplishTaskComplete);
		}
		
		private function onAccomplishTaskComplete(event:ServerEvent):void
		{
			var taskIndex:int = taskDialog.currentSelectedTaskItem.taskIndex;
			var taskConfig:TaskConfig = Config.taskConfigs[taskIndex];
			var awardItems:Array = new Array();
			for (var i:int = 0; i < taskConfig.awardObj.length; i++)
			{
				var awardItem:GiftItem = new GiftItem([taskConfig.awardObj[i]]);
				addChild(awardItem);
				awardItems.push(awardItem);
			}
			var awardMessage:AwardMessage = new AwardMessage("恭喜你完成了任务，获得以下奖励", "", awardItems);
			WidgetManager.getInstance().popOut([awardMessage], true, true);
			ServerHelper.getInstance().removeEventListener(ServerEvent.ACCOMP_TASK, onAccomplishTaskComplete);
			taskDialog.refresh();
			if (taskConfig.feedFlag)
			{
				ServerHelper.sendFeed("完成了任务“" + taskConfig.name + "”");
			}
		}
	}
}